/*
 * NewAdocDialog.java
 *
 * Created on 2010-04-11, 14.31.56
 */

package adoc;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author ILikePancakes
 */
public class NewAdocDialog extends javax.swing.JDialog {
    private int attachmentStep = 1;
    private int appendixStep = 1;
    private ArrayList<javax.swing.JTextField> attachments = new ArrayList();
    private ArrayList<javax.swing.JTextField> appendices = new ArrayList();
    private ArrayList<javax.swing.JButton> attachmentButtons = new ArrayList();
    private ArrayList<javax.swing.JButton> appendixButtons = new ArrayList();
    private ArrayList<javax.swing.JCheckBox> appendixCheckBoxes = new ArrayList();
    private ArrayList<javax.swing.JSpinner> appendixSpinners = new ArrayList();

    /** Creates new form NewAdocDialog */
    public NewAdocDialog() {
        super(AdocApp.getApplication().getMainFrame(), true);
        initComponents();
        adocFileChooser.setFileView(new AdocFileView());
        adocFileChooser.setFileFilter(new AdocFileFilter());
        attachments.add(attachField);
        appendices.add(appendixField);
        attachmentButtons.add(attachButton);
        appendixButtons.add(appendixButton);
        appendixCheckBoxes.add(specificCheckBox);
        appendixSpinners.add(specificSpinner);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jCheckBox1 = new javax.swing.JCheckBox();
        newPanel = new javax.swing.JTabbedPane();
        attributesScrollPane = new javax.swing.JScrollPane();
        attributesTab = new javax.swing.JPanel();
        authorPanel = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        addressLabel = new javax.swing.JLabel();
        addressField = new javax.swing.JTextField();
        authorField = new javax.swing.JTextField();
        mainDocPanel = new javax.swing.JPanel();
        documentField = new javax.swing.JTextField();
        documentChooser = new javax.swing.JButton();
        appendicesScrollPane = new javax.swing.JScrollPane();
        appendicesTab = new javax.swing.JPanel();
        appendixLabel = new javax.swing.JLabel();
        appendixButton = new javax.swing.JButton();
        appendixField = new javax.swing.JTextField();
        addAppendixLabel = new javax.swing.JLabel();
        specificCheckBox = new javax.swing.JCheckBox();
        specificSpinner = new javax.swing.JSpinner();
        attachmentsScrollPane = new javax.swing.JScrollPane();
        attachmentsTab = new javax.swing.JPanel();
        attachButton = new javax.swing.JButton();
        attachLabel = new javax.swing.JLabel();
        attachField = new javax.swing.JTextField();
        addAttachmentsLabel = new javax.swing.JLabel();
        adocFileChooser = new javax.swing.JFileChooser();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(adoc.AdocApp.class).getContext().getResourceMap(NewAdocDialog.class);
        jCheckBox1.setText(resourceMap.getString("jCheckBox1.text")); // NOI18N
        jCheckBox1.setName("jCheckBox1"); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        newPanel.setName("newPanel"); // NOI18N

        attributesScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        attributesScrollPane.setName("attributesScrollPane"); // NOI18N

        attributesTab.setName("attributesTab"); // NOI18N

        authorPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("authorPanel.border.title"))); // NOI18N
        authorPanel.setName("authorPanel"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        addressLabel.setText(resourceMap.getString("addressLabel.text")); // NOI18N
        addressLabel.setName("addressLabel"); // NOI18N

        addressField.setText(resourceMap.getString("addressField.text")); // NOI18N
        addressField.setName("addressField"); // NOI18N

        authorField.setName("authorField"); // NOI18N

        javax.swing.GroupLayout authorPanelLayout = new javax.swing.GroupLayout(authorPanel);
        authorPanel.setLayout(authorPanelLayout);
        authorPanelLayout.setHorizontalGroup(
            authorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(authorPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(authorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(addressLabel))
                .addGap(84, 84, 84)
                .addGroup(authorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(addressField)
                    .addComponent(authorField, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        authorPanelLayout.setVerticalGroup(
            authorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(authorPanelLayout.createSequentialGroup()
                .addGroup(authorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(authorField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(authorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addressLabel)
                    .addComponent(addressField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        mainDocPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("mainDocPanel.border.title"))); // NOI18N
        mainDocPanel.setName("mainDocPanel"); // NOI18N

        documentField.setEditable(false);
        documentField.setName("documentField"); // NOI18N

        documentChooser.setText(resourceMap.getString("documentChooser.text")); // NOI18N
        documentChooser.setName("documentChooser"); // NOI18N
        documentChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                documentChooserActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout mainDocPanelLayout = new javax.swing.GroupLayout(mainDocPanel);
        mainDocPanel.setLayout(mainDocPanelLayout);
        mainDocPanelLayout.setHorizontalGroup(
            mainDocPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainDocPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(documentField, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(documentChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        mainDocPanelLayout.setVerticalGroup(
            mainDocPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainDocPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainDocPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(documentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(documentChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout attributesTabLayout = new javax.swing.GroupLayout(attributesTab);
        attributesTab.setLayout(attributesTabLayout);
        attributesTabLayout.setHorizontalGroup(
            attributesTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(attributesTabLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(attributesTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(mainDocPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(authorPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        attributesTabLayout.setVerticalGroup(
            attributesTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(attributesTabLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(authorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(mainDocPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(159, Short.MAX_VALUE))
        );

        attributesScrollPane.setViewportView(attributesTab);

        newPanel.addTab(resourceMap.getString("attributesScrollPane.TabConstraints.tabTitle"), attributesScrollPane); // NOI18N

        appendicesScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        appendicesScrollPane.setName("appendicesScrollPane"); // NOI18N

        appendicesTab.setName("appendicesTab"); // NOI18N

        appendixLabel.setText(resourceMap.getString("appendixLabel.text")); // NOI18N
        appendixLabel.setName("appendixLabel"); // NOI18N

        appendixButton.setText(resourceMap.getString("appendixButton.text")); // NOI18N
        appendixButton.setName("appendixButton"); // NOI18N
        appendixButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                appendixButtonActionPerformed(evt);
            }
        });

        appendixField.setEditable(false);
        appendixField.setName("appendixField"); // NOI18N

        addAppendixLabel.setForeground(resourceMap.getColor("addAppendixLabel.foreground")); // NOI18N
        addAppendixLabel.setText(resourceMap.getString("addAppendixLabel.text")); // NOI18N
        addAppendixLabel.setName("addAppendixLabel"); // NOI18N
        addAppendixLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                addAppendixLabelMouseClicked(evt);
            }
        });

        specificCheckBox.setText(resourceMap.getString("specificCheckBox.text")); // NOI18N
        specificCheckBox.setName("specificCheckBox"); // NOI18N
        specificCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                specificCheckBoxActionPerformed(evt);
            }
        });

        specificSpinner.setEnabled(false);
        specificSpinner.setName("specificSpinner"); // NOI18N

        javax.swing.GroupLayout appendicesTabLayout = new javax.swing.GroupLayout(appendicesTab);
        appendicesTab.setLayout(appendicesTabLayout);
        appendicesTabLayout.setHorizontalGroup(
            appendicesTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(appendicesTabLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(appendicesTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(addAppendixLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(appendixLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(appendixField, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(appendixButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(specificCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(specificSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );
        appendicesTabLayout.setVerticalGroup(
            appendicesTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(appendicesTabLayout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(addAppendixLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(appendicesTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(appendixLabel)
                    .addComponent(appendixField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(appendixButton)
                    .addComponent(specificCheckBox)
                    .addComponent(specificSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        appendicesScrollPane.setViewportView(appendicesTab);

        newPanel.addTab(resourceMap.getString("appendicesScrollPane.TabConstraints.tabTitle"), appendicesScrollPane); // NOI18N

        attachmentsScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        attachmentsScrollPane.setName("attachmentsScrollPane"); // NOI18N

        attachmentsTab.setName("attachmentsTab"); // NOI18N

        attachButton.setText(resourceMap.getString("attachButton.text")); // NOI18N
        attachButton.setName("attachButton"); // NOI18N
        attachButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                attachButtonActionPerformed(evt);
            }
        });

        attachLabel.setText(resourceMap.getString("attachLabel.text")); // NOI18N
        attachLabel.setName("attachLabel"); // NOI18N

        attachField.setEditable(false);
        attachField.setName("attachField"); // NOI18N

        addAttachmentsLabel.setFont(resourceMap.getFont("addAttachmentsLabel.font")); // NOI18N
        addAttachmentsLabel.setForeground(resourceMap.getColor("addAttachmentsLabel.foreground")); // NOI18N
        addAttachmentsLabel.setText(resourceMap.getString("addAttachmentsLabel.text")); // NOI18N
        addAttachmentsLabel.setName("addAttachmentsLabel"); // NOI18N
        addAttachmentsLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                addAttachmentsLabelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout attachmentsTabLayout = new javax.swing.GroupLayout(attachmentsTab);
        attachmentsTab.setLayout(attachmentsTabLayout);
        attachmentsTabLayout.setHorizontalGroup(
            attachmentsTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(attachmentsTabLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(attachmentsTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(addAttachmentsLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(attachLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(5, 5, 5)
                .addComponent(attachField, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(attachButton)
                .addContainerGap(89, Short.MAX_VALUE))
        );
        attachmentsTabLayout.setVerticalGroup(
            attachmentsTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(attachmentsTabLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(addAttachmentsLabel)
                .addGap(12, 12, 12)
                .addGroup(attachmentsTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(attachLabel)
                    .addComponent(attachField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(attachButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        attachmentsScrollPane.setViewportView(attachmentsTab);

        newPanel.addTab(resourceMap.getString("attachmentsScrollPane.TabConstraints.tabTitle"), attachmentsScrollPane); // NOI18N

        adocFileChooser.setAcceptAllFileFilterUsed(false);
        adocFileChooser.setApproveButtonText(resourceMap.getString("adocFileChooser.approveButtonText")); // NOI18N
        adocFileChooser.setDialogTitle(resourceMap.getString("adocFileChooser.dialogTitle")); // NOI18N
        adocFileChooser.setDialogType(javax.swing.JFileChooser.CUSTOM_DIALOG);
        adocFileChooser.setName("adocFileChooser"); // NOI18N
        adocFileChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adocFileChooserActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(adocFileChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 464, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(newPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(adocFileChooser, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(newPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                .addGap(11, 11, 11))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void appendixButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_appendixButtonActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new DocumentFileFilter());
        chooser.setAcceptAllFileFilterUsed(false);
        AdocApp.getApplication().getMainFrame().setLocationRelativeTo(chooser);
        chooser.showDialog(AdocApp.getApplication().getMainFrame(), "Add");
        if (chooser.getSelectedFile() != null) appendices.get(appendixButtons.indexOf((evt.getSource()))).setText(chooser.getSelectedFile().getPath());
}//GEN-LAST:event_appendixButtonActionPerformed

    private void attachButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_attachButtonActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new AdocFileFilter());
        chooser.setAcceptAllFileFilterUsed(false);
        AdocApp.getApplication().getMainFrame().setLocationRelativeTo(chooser);
        chooser.showDialog(AdocApp.getApplication().getMainFrame(), "Add");
        if (chooser.getSelectedFile() != null) attachments.get(attachmentButtons.indexOf((evt.getSource()))).setText(chooser.getSelectedFile().getPath());
}//GEN-LAST:event_attachButtonActionPerformed

    private void documentChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_documentChooserActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new DocumentFileFilter());
        chooser.setAcceptAllFileFilterUsed(false);
        AdocApp.getApplication().getMainFrame().setLocationRelativeTo(chooser);
        chooser.showDialog(AdocApp.getApplication().getMainFrame(), "Add");
        if (chooser.getSelectedFile() != null) documentField.setText(chooser.getSelectedFile().getPath());
}//GEN-LAST:event_documentChooserActionPerformed

    private void adocFileChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adocFileChooserActionPerformed
        if (evt.getActionCommand().equals(JFileChooser.CANCEL_SELECTION)) {
            this.dispose();
        } else {
            try {
                // gauti priedu priedu map'a
                HashMap<String, ArrayList> appendixMap = this.getAppendixMap();
                // surinkti visus priedus, kurie yra kitu priedu priedai ir todel nera "tikri" priedai
                ArrayList<String> skippable = new ArrayList();
                for (ArrayList list : appendixMap.values()) { skippable.addAll(list); }
                String[] appendices = this.getAppendices(skippable);
                String[] attachments = this.getAttachments();
                String mainDocument = documentField.getText();
                String authorName = authorField.getText();
                String authorAddress = addressField.getText();
                File file = adocFileChooser.getSelectedFile();
                // tikrinimas
                if (appendices != null)
                    for (String appendix : appendices) {
                        String mime = AdocUtilities.getRealMimeType(appendix);
                        String extensionMime = AdocUtilities.getMimeByExtension(AdocUtilities.getExtension(appendix));
                        if (!AdocUtilities.isValidDocumentType(mime) || !extensionMime.equals(mime)) throw new Exception(appendix+" nėra leistino formato arba pažeistas");
                    }
                for (ArrayList list : appendixMap.values()) { 
                    for (Object o : list) {
                        String mime = AdocUtilities.getRealMimeType(o.toString());
                        String extensionMime = AdocUtilities.getMimeByExtension(AdocUtilities.getExtension(o.toString()));
                        if (!AdocUtilities.isValidDocumentType(mime) || !extensionMime.equals(mime)) throw new Exception(o+" nėra leistino formato arba pažeistas");
                    }
                }
                if (attachments != null)
                    for (String attachment : attachments) {
                        ArrayList<String> response = AdocUtilities.checkADOC(attachment);
                        if (response.size() > 0) throw new Exception(attachment + " nėra validus adoc dokumentas");
                    }
                if (mainDocument.length() == 0) throw new Exception("nenurodytas pagrindinis dokumentas");
                else {
                    String mime = AdocUtilities.getRealMimeType(mainDocument);
                    String extensionMime = AdocUtilities.getMimeByExtension(AdocUtilities.getExtension(mainDocument));
                    if (!AdocUtilities.isValidDocumentType(mime) || !extensionMime.equals(mime)) throw new Exception(mainDocument+" nėra leistino formato arba pažeistas");
                }
                if (authorName.length() == 0) throw new Exception("nenurodytas authoriaus vardas");
                if (authorAddress.length() == 0) throw new Exception("nenurodytas autoriaus adresas");
                if (file == null) throw new Exception("nenurodytas naujo adoc failo vardas");
                AdocUtilities.createADOC(file.getPath()+".adoc", new String[] {authorName, authorAddress},
                        mainDocument, appendices,
                        attachments, appendixMap);
                JOptionPane.showMessageDialog(this, "ADOC Successfully Created!", "Success!", JOptionPane.INFORMATION_MESSAGE);
                this.dispose();
            } catch (Throwable ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(this,
                ex.getMessage(),
                "Error While Creating ADOC",
                JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_adocFileChooserActionPerformed

    private void addAttachmentsLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_addAttachmentsLabelMouseClicked
        javax.swing.JButton newAttachButton = new javax.swing.JButton();
        javax.swing.JLabel newAttachLabel = new javax.swing.JLabel();
        javax.swing.JTextField newAttachField = new javax.swing.JTextField();

        org.jdesktop.application.ResourceMap resourceMap = this.getResourceMap();
        newAttachButton.setText(resourceMap.getString("attachButton.text")); // NOI18N
        newAttachButton.setName("attachButton"); // NOI18N

        newAttachLabel.setText("Select Attachment "+(attachmentStep + 1)); // NOI18N
        newAttachLabel.setName("attachLabel"); // NOI18N

        newAttachField.setName("attachField");
        newAttachField.setEditable(false);
        Rectangle rec = attachLabel.getBounds();
        newAttachLabel.setBounds(rec.x, rec.y + (attachLabel.getHeight() + 15) * attachmentStep, rec.width, rec.height);
        rec = attachButton.getBounds();
        newAttachButton.setBounds(rec.x, rec.y + (attachButton.getHeight() + 6) * attachmentStep, rec.width, rec.height);
        rec = attachField.getBounds();
        newAttachField.setBounds(rec.x, rec.y + (attachField.getHeight() + 9) * attachmentStep, rec.width, rec.height);
        attachmentsTab.add(newAttachButton);
        attachmentsTab.add(newAttachLabel);
        attachmentsTab.add(newAttachField);
        attachmentsTab.repaint();
        attachmentStep++;
        attachments.add(newAttachField);
        attachmentButtons.add(newAttachButton);

        newAttachButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                attachButtonActionPerformed(evt);
            }
        });

        Dimension pref = attachmentsTab.getPreferredSize();
        pref.height += attachButton.getHeight() + 6;
        attachmentsTab.setPreferredSize(pref);
        newPanel.repaint();
    }//GEN-LAST:event_addAttachmentsLabelMouseClicked


    private void addAppendixLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_addAppendixLabelMouseClicked
        javax.swing.JButton newAppendixButton = new javax.swing.JButton();
        javax.swing.JLabel newAppendixLabel = new javax.swing.JLabel();
        javax.swing.JTextField newAppendixField = new javax.swing.JTextField();
        javax.swing.JCheckBox newSpecificCheckBox = new javax.swing.JCheckBox();
        javax.swing.JSpinner newSpecificSpinner = new javax.swing.JSpinner();

        org.jdesktop.application.ResourceMap resourceMap = this.getResourceMap();
        newAppendixButton.setText(resourceMap.getString("appendixButton.text")); // NOI18N
        newAppendixButton.setName("appendixButton"); // NOI18N

        newAppendixLabel.setText("Select Appendix "+(appendixStep + 1)); // NOI18N
        newAppendixLabel.setName("appendixLabel"); // NOI18N

        newAppendixField.setName("appendixField");
        newAppendixField.setEditable(false);

        newSpecificCheckBox.setText(resourceMap.getString("specificCheckBox.text")); // NOI18N
        newSpecificCheckBox.setName("specificCheckBox"); // NOI18N
        newSpecificCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                specificCheckBoxActionPerformed(evt);
            }
        });

        newSpecificSpinner.setEnabled(false);
        newSpecificSpinner.setName("specificSpinner"); // NOI18N

        Rectangle rec = appendixLabel.getBounds();
        newAppendixLabel.setBounds(rec.x, rec.y + (appendixLabel.getHeight() + 15) * appendixStep, rec.width, rec.height);
        rec = appendixButton.getBounds();
        newAppendixButton.setBounds(rec.x, rec.y + (appendixButton.getHeight() + 6) * appendixStep, rec.width, rec.height);
        rec = appendixField.getBounds();
        newAppendixField.setBounds(rec.x, rec.y + (appendixField.getHeight() + 9) * appendixStep, rec.width, rec.height);
        rec = specificCheckBox.getBounds();
        newSpecificCheckBox.setBounds(rec.x, rec.y + (specificCheckBox.getHeight() + 6) * appendixStep, rec.width, rec.height);
        rec = specificSpinner.getBounds();
        newSpecificSpinner.setBounds(rec.x, rec.y + (specificSpinner.getHeight() + 9) * appendixStep, rec.width, rec.height);

        appendicesTab.add(newAppendixButton);
        appendicesTab.add(newAppendixLabel);
        appendicesTab.add(newAppendixField);
        appendicesTab.add(newSpecificCheckBox);
        appendicesTab.add(newSpecificSpinner);
        appendicesTab.repaint();
        appendixStep++;

        appendices.add(newAppendixField);
        appendixButtons.add(newAppendixButton);
        appendixCheckBoxes.add(newSpecificCheckBox);
        appendixSpinners.add(newSpecificSpinner);

        newAppendixButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                appendixButtonActionPerformed(evt);
            }
        });

        Dimension pref = appendicesTab.getPreferredSize();
        pref.height += attachButton.getHeight() + 6;
        appendicesTab.setPreferredSize(pref);
        newPanel.repaint();

        for (javax.swing.JSpinner spinner : appendixSpinners) {
            spinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, appendixSpinners.size(), 1));
        }
    }//GEN-LAST:event_addAppendixLabelMouseClicked

    private void specificCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_specificCheckBoxActionPerformed
        javax.swing.JSpinner spinner = appendixSpinners.get(appendixCheckBoxes.indexOf(evt.getSource()));
        if (((JCheckBox) evt.getSource()).isSelected()) spinner.setEnabled(true);
        else { spinner.setEnabled(false); spinner.setValue(0); }
    }//GEN-LAST:event_specificCheckBoxActionPerformed

    private org.jdesktop.application.ResourceMap getResourceMap() {
        return org.jdesktop.application.Application.getInstance(adoc.AdocApp.class).getContext().getResourceMap(NewAdocDialog.class);
    }

    private String[] getAppendices() {
        ArrayList<String> tmpArray = new ArrayList();
        for (JTextField appendix : appendices) if (!appendix.getText().equals("")) tmpArray.add(appendix.getText());
        if (tmpArray.size() == 0) return null;
        String[] appendixArray = new String[tmpArray.size()];
        int i = 0;
        for (String appendix : tmpArray) {
            appendixArray[i] = appendix;
            i++;
        }
        return appendixArray;
    }

    private String[] getAppendices(ArrayList<String> skippable) {
        ArrayList<String> tmpArray = new ArrayList();
        for (JTextField appendix : appendices) if (!appendix.getText().equals("") && !skippable.contains(appendix.getText())) tmpArray.add(appendix.getText());
        if (tmpArray.size() == 0) return null;
        String[] appendixArray = new String[tmpArray.size()];
        int i = 0;
        for (String appendix : tmpArray) {
            appendixArray[i] = appendix;
            i++;
        }
        return appendixArray;
    }

    private String[] getAttachments() {
        ArrayList<String> tmpArray = new ArrayList();
        for (JTextField attachment : attachments) if (!attachment.getText().equals("")) tmpArray.add(attachment.getText());
        if (tmpArray.size() == 0) return null;
        String[] attachmentArray = new String[tmpArray.size()];
        int i = 0;
        for (String attachment : tmpArray) {
            attachmentArray[i] = attachment;
            i++;
        }
        return attachmentArray;
    }

    private HashMap<String, ArrayList> getAppendixMap() throws Exception {
        HashMap<String, ArrayList> appendixMap = new HashMap();
        // kiekvienam is appendix
        for (int i = 0; i < appendices.size(); i++) {
            String appendixName = appendices.get(i).getText();
            // eiti per likusius ir ieskoti tu, kurie yra jo priedai
            for (int j = 0; j < appendices.size(); j++) {
                if (j == i) continue;
                if (appendixCheckBoxes.get(j).isSelected() && appendixSpinners.get(j).getValue().toString().equals((i+1)+"")) {
                    int k = i;
                    while (true) {
                        if (!appendixCheckBoxes.get(k).isSelected() || appendixSpinners.get(k).getValue().toString().equals("0")) break;
                        k = Integer.parseInt(appendixSpinners.get(k).getValue().toString())-1;
                        if (k == i) throw new Exception("Negali buti ciklu");
                    }
                    //if (appendixCheckBoxes.get(i).isSelected()) throw new Exception("Priedas gali būti tik pagrindiniam dokumentui arba kitam priedui");
                    if (appendixMap.get(appendixName) == null) appendixMap.put(appendixName, new ArrayList());
                    appendixMap.get(appendixName).add(appendices.get(j).getText());
                }
            }
        }
        return appendixMap;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel addAppendixLabel;
    private javax.swing.JLabel addAttachmentsLabel;
    private javax.swing.JTextField addressField;
    private javax.swing.JLabel addressLabel;
    private javax.swing.JFileChooser adocFileChooser;
    private javax.swing.JScrollPane appendicesScrollPane;
    private javax.swing.JPanel appendicesTab;
    private javax.swing.JButton appendixButton;
    private javax.swing.JTextField appendixField;
    private javax.swing.JLabel appendixLabel;
    private javax.swing.JButton attachButton;
    private javax.swing.JTextField attachField;
    private javax.swing.JLabel attachLabel;
    private javax.swing.JScrollPane attachmentsScrollPane;
    private javax.swing.JPanel attachmentsTab;
    private javax.swing.JScrollPane attributesScrollPane;
    private javax.swing.JPanel attributesTab;
    private javax.swing.JTextField authorField;
    private javax.swing.JPanel authorPanel;
    private javax.swing.JButton documentChooser;
    private javax.swing.JTextField documentField;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel mainDocPanel;
    private javax.swing.JTabbedPane newPanel;
    private javax.swing.JCheckBox specificCheckBox;
    private javax.swing.JSpinner specificSpinner;
    // End of variables declaration//GEN-END:variables

}
